#!/usr/bin/perl

use strict 'vars';
package main;

use Getopt::Std;
use Cwd;

use vars qw($opt_p $opt_P);

#get command line options
getopts('p:P:');
if ($opt_p eq "" || $opt_P eq "") {
    die "Usage: kill-process.pl -p <port> -P <process name>\n";
}

open RELEVANT_PS, "ps x| grep $opt_p|grep $opt_P|grep -v grep|grep -v kill|";

my $ps; my @arr; my $cmd;
while ($ps = <RELEVANT_PS>) {
    @arr = split(/ +/, $ps);
    if ($arr[0] eq "")	{ $cmd = "kill -9 $arr[1]" }
    else		{ $cmd = "kill -9 $arr[0]";}
    print $cmd."\n";
    system($cmd);
}
